package com.iotek.ssm.controller;

import com.iotek.ssm.pojo.Position;
import com.iotek.ssm.pojo.Section;
import com.iotek.ssm.service.SectionService;
import com.iotek.ssm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/section")
public class SectionController {
    @Autowired
    private SectionService sectionService;

    @Autowired
    private Util util;

    @RequestMapping("/checkSection")
    public @ResponseBody int checkSection(String section){
        List<Section> sections = null;
        try {
            sections = sectionService.findSections();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sections != null)
            for (Section se:sections)
                if (se.getSection().equals(section))
                    return 1;
        return 0;
    }

    @RequestMapping("/checkPosition")
    public @ResponseBody int checkPosition(Position position) {
        List<Position> positions = null;
        try {
            positions = sectionService.findPositions(position.getSection().getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (positions != null)
            for (Position po : positions)
                if (po.getPosition().equals(position.getPosition()))
                    return 1;
        return 0;
    }

    @RequestMapping("/sections")
    public String sections() {
        return "admin/addSection";
    }

    @RequestMapping("/positions")
    public String positions(Model model, Integer sId) {
        Section section = null;
        List<Position> positions = null;
        try {
            section = sectionService.findSection(sId);
            positions = sectionService.findPositions(sId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("section", section);
        model.addAttribute("positions", positions);
        return "admin/addPosition";
    }

    @RequestMapping("/addSection")
    public String addSection(String section, Model model) {
        try {
            sectionService.addSection(section);
            util.setJumpDetails(model, "添加成功！", "进入管理主页", "../admin/goBackStage", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "jumpPage";
    }

    @RequestMapping("/addPosition")
    public String addPosition(Position position, Model model) {
        try {
            sectionService.addPosition(position);
            util.setJumpDetails(model, "添加成功！", "进入管理主页", "../admin/goBackStage", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "jumpPage";
    }

    @RequestMapping("/updateSection")
    @ResponseBody
    public int updateSection(Section section) {
        try {
            sectionService.updateSection(section);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/updatePosition")
    @ResponseBody
    public int updatePosition(Position position) {
        try {
            sectionService.updatePosition(position);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delSection")
    @ResponseBody
    public int delSection(Integer sectionId) {
        try {
            sectionService.delSectionAndPositions(sectionId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delPosition")
    @ResponseBody
    public int delPosition(Integer positionId) {
        try {
            sectionService.delPosition(positionId);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @ModelAttribute("sections")
    public List<Section> findSections() {
        List<Section> sections = null;
        try {
            sections = sectionService.findSections();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sections;
    }

}
